﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace WindowsFormProject.Personel
{
    public partial class PersonelGirisCikisFrm : Form
    {
        public PersonelGirisCikisFrm()
        {
            InitializeComponent();
        }
        DBBaglantiDataContext db = new DBBaglantiDataContext();

        private void PersonelGirisCikisFrm_Load(object sender, EventArgs e)
        {

            Depo.GenelIslemler.EnterIlerleload(this.Name);
            SubeID.DataSource = (from x in db.Subelers select x).ToList();
            SubeID.DisplayMember = "SubeAdi";
            SubeID.ValueMember = "SubeID";
            SubeID.SelectedValue = 1;
            GridViewDefaulSetValue();           
        }
        private void GridViewDefaulSetValue()
        {

            //            this.GrdPersonelGirisCikis.Columns["PersonelGunlukGirisTarihSaat"].DefaultCellStyle.Format = "dd/mm/yyyy";
            //this.GrdPersonelGirisCikis.Columns["PersonelGunlukCikisTarihSaat"].DefaultCellStyle.Format = "dd/mm/yyyy";

        }



        private void GrdPersonelGirisCikis_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                PersonelListesi PersonelListesi = new Personel.PersonelListesi(e.RowIndex);
                PersonelListesi.AcanForm = this;
                PersonelListesi.Show();
            }
        }

        private void GrdPersonelGirisCikis_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            GrdPersonelGirisCikis[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = Color.Red;
            GrdPersonelGirisCikis[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Black;

        }

     
        bool kayitkontrol = false;
        int satirno = -1;

        private void GrdPersonelGirisCikis_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                foreach (DataGridViewTextBoxCell item in GrdPersonelGirisCikis.SelectedCells)
                {
                    if (item.ColumnIndex == 0)
                    {
                        var kayitvarmi = (from x in db.PersonelBilgileris where x.PersonelID == GrdPersonelGirisCikis.Rows[e.RowIndex].Cells[0].Value.ToString() && x.PersonelSubeID == Convert.ToInt32(SubeID.SelectedValue) select new { x.PersonelID, x.PersonelAdi, x.PersonelSoyadi }).FirstOrDefault();

                        if (kayitvarmi != null)
                        {
                            if (GrdPersonelGirisCikis.Rows[e.RowIndex].Cells[0].Value != null)
                            {
                                kayitkontrol = true;
                                satirno = e.RowIndex;
                                GrdPersonelGirisCikis.Rows[e.RowIndex].Cells[1].Value = kayitvarmi.PersonelAdi + " " + kayitvarmi.PersonelSoyadi;
                                return;
                            }
                        }
                        else
                        {
                            kayitkontrol = false;
                            MessageBox.Show("Girilen Kod Mevcut değil");
                        }

                    }

                    if (item.RowIndex == satirno)
                    {
                        if (kayitkontrol == true)
                        {
                            Pgt.PersonelGunlukGirisTarihSaat = Convert.ToDateTime(GrdPersonelGirisCikis.Rows[e.RowIndex].Cells[2].Value.ToString());
                            Pgt.PersonelGunlukCikisTarihSaat = Convert.ToDateTime(GrdPersonelGirisCikis.Rows[e.RowIndex].Cells[3].Value.ToString());


                        }
                    }

                }

            }
            catch (Exception)
            {

            }
        }

        private void bt_kaydet_Click(object sender, EventArgs e)
        {
            //db.PersonelGunlukTakips.InsertAllOnSubmit(TakipListesi);
            db.SubmitChanges();
            GrdPersonelGirisCikis.Rows.Clear();
            MessageBox.Show("Kayit Başarılı");



        }


        private void dttarih1_KeyDown(object sender, KeyEventArgs e)
        {

      //  Depo.GenelIslemler.Enterileilerle(this.Name,e);
        }

        private void SubeID_KeyDown(object sender, KeyEventArgs e)
        {
         //   Depo.GenelIslemler.Enterileilerle(this.Name, e);
        }


        private void GrdPersonelGirisCikis_KeyDown(object sender, KeyEventArgs e)
        {
            {

                if (e.KeyCode == Keys.Enter)
                {
                    DataGridView vw = (DataGridView)sender;

                    if (vw.CurrentCell.RowIndex == vw.Rows.Count - 1)
                    {
                        if (vw.CurrentCell.ColumnIndex != vw.ColumnCount - 1)
                        {
                            vw.CurrentCell = vw.Rows[vw.CurrentCell.RowIndex].Cells[vw.CurrentCell.ColumnIndex + 1];
                        }
                        else
                        {
                            vw.Rows.Add(1);
                            vw.Refresh();
                            SendKeys.Send("{HOME}");
                        }
                    }
                    else
                    {

                        if (vw.CurrentCell.ColumnIndex == vw.ColumnCount - 1)
                        {
                            vw.CurrentCell = vw.Rows[vw.CurrentCell.RowIndex + 1].Cells[0];
                        }
                        else
                        {

                            vw.CurrentCell = vw.Rows[vw.CurrentCell.RowIndex].Cells[vw.CurrentCell.ColumnIndex + 1];
                        }
                    }

                    e.Handled = true;
                }
            }


        }

        private void GrdPersonelGirisCikis_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            try
            {

                if (kayitkontrol == true && e.RowIndex == satirno)
                {
                    Pgt.PersonelID = GrdPersonelGirisCikis.Rows[e.RowIndex].Cells[0].Value.ToString();
                    Pgt.PersonelGunlukGirisTarihSaat = Convert.ToDateTime(GrdPersonelGirisCikis.Rows[e.RowIndex].Cells[2].Value.ToString());
                    Pgt.PersonelGunlukCikisTarihSaat = Convert.ToDateTime(GrdPersonelGirisCikis.Rows[e.RowIndex].Cells[2].Value.ToString());
                   // TakipListesi.Add(Pgt);
                    kayitkontrol = false;
                    satirno = -1;

                }
            }
            catch
            { }
        }
       // List<IEnumerable> TakipListesi = new List<IEnumerable>();
        PersonelGunlukTakip Pgt = new PersonelGunlukTakip();

        private void SubeID_Leave(object sender, EventArgs e)
        {

            var kayitgetir = (from x in db.PersonelGunlukTakips
                              where x.PersonelGunlukGirisTarihSaat >= Convert.ToDateTime(dttarih1.Text) & x.PersonelGunlukCikisTarihSaat <= Convert.ToDateTime(dtime2.Text) &
                                  x.PersonelBilgileri.PersonelSubeID == Convert.ToInt32(SubeID.SelectedValue)
                              select new { x.PersonelID, PersonelAdSoyad = x.PersonelBilgileri.PersonelAdi + " " + x.PersonelBilgileri.PersonelSoyadi, x.PersonelGunlukGirisTarihSaat, x.PersonelGunlukCikisTarihSaat }).ToList();

            if (kayitgetir.Count != 0)
            {

                GrdPersonelGirisCikis.DataSource = kayitgetir;
                GrdPersonelGirisCikis.AllowUserToAddRows = true;

            }
        }

        private void GrdPersnelGirisCikis_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            }

        private void PersonelGirisCikisFrm_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        



        //public DataGridView PersonelTakipDetaylari(string dt1, string dt2, int Sube)
        //{
        //    DataGridView ReturnValue;

        //    DataGridView dg = new DataGridView();

        //    var sorgu = (from x in db.PersonelGunlukTakips
        //                 join y in db.PersonelBilgileris on x.PersonelID equals y.PersonelID
        //                 where x.PersonelGunlukGirisTarihSaat >= Convert.ToDateTime(dt1) & x.PersonelGunlukCikisTarihSaat <= Convert.ToDateTime(dt2) &
        //                     y.PersonelSubeID == Convert.ToInt32(Sube)
        //                 select new { x.PersonelID, y.PersonelAdi, x.PersonelGunlukGirisTarihSaat, x.PersonelGunlukCikisTarihSaat }).ToList();


        //    dg.DataSource = sorgu;
        //    ReturnValue = dg;

        //    return ReturnValue;


        //}


        //private void SubeID_Leave(object sender, EventArgs e)
        //{



        //    GrdPersonelGirisCikis.DataSource = PersonelTakipDetaylari(dttarih1.Text.ToString(), dtime2.Text.ToString(), Convert.ToInt32(SubeID.SelectedValue));



        //}




    }

}
